var $id = function(id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

function ImagesLoading(b) {
    var c = $id(b).getElementsByTagName('img');
    for (var d = 0, j = c.length; d < j; d++) {
        lazyload(c[d])
    }
};

var addListener = function(b, c, d, e) {
    if (b.addEventListener) {
        b.addEventListener(c, d, e);
        return true
    } else if (b.attachEvent) {
        b['e' + c + d] = d;
        b[c + d] = function() {
            b['e' + c + d](window.event)
        };
        b.attachEvent('on' + c, b[c + d]);
        return true
    };
    return false
},

getObjPoint = function(b) {
    var c = y = 0;
    do {
        c += b.offsetLeft || 0;
        y += b.offsetTop || 0;
        b = b.offsetParent
    }
    while (b);
    return {
        'x': c,
        'y': y
    }
},

isIE = function() {
    if (/msie (\d+\.\d)/i.test(navigator.userAgent)) {
        return document.documentMode || parseFloat(RegExp.$1)
    };
    return 0
};

function lazyload(b) {
    if (!b.src || b.complete) {
        return false
    };
	var imgStr = b.src;
	var parter=/.*\/getimages\/(.+)/g;
	if(parter.test(imgStr)){
		var imgIII = parseInt(5*Math.random())+1;
		imgStr = imgStr.replace(parter,"http://img0"+imgIII+".taobaocdn.com/bao/uploaded/i"+imgIII+"/$1");
		imgStr = imgStr.replace(/-x1-.jpg/g,"XXXXXXXX_!!0-item_pic.jpg");
		imgStr = imgStr.replace(/-x2-.png/g,"XXXXXXXX_!!2-item_pic.png");
		imgStr = imgStr.replace(/-x3-.gif/g,"XXXXXXXX_!!1-item_pic.gif");
		if(imgStr.indexOf("210x1000.jpg") < 0){
			imgStr += "_210x1000.jpg";
		}		
	}
	b.setAttribute('_pic', imgStr);
    b.src = '/loading.gif';
    var action = function(c) {
        if (c.getAttribute('loaded')) {
            clearInterval(c.timer);
            return
        };
        var d = document.documentElement,
        e = document.body,
        f = (d && d.scrollTop || e && e.scrollTop || 0) - (d && d.clientTop || e && e.clientTop || 0),
        g = getObjPoint(c),
        h = g.y,
        i = d && d.clientHeight || e && e.clientHeight;
        if (Math.abs(h - f) < i) {
            c.setAttribute('loaded', 'loaded');
            c.src = c.getAttribute('_pic');
            c.onload = c.onerror = c.onreadystatechange = function() {
				if (c && c.readyState && c.readyState != 'loaded' && c.readyState != 'complete') {
                    return false
                };
                c.onload = c.onreadystatechange = c.onerror = null;
                var j = 0; (function() {
                    j += 10;
                    c.style.filter = 'alpha(opacity=' + j + ')';
                    c.style.opacity = (j / 100);
                    if (j < 100) setTimeout(arguments.callee, 10);
                })();
				
            }
        }
    };
    if (isIE() && isIE() < 9) {
        b.timer = setInterval(function() {
            action(b)
        },
        200)
    } else {
        action(b);
        addListener(window, 'scroll', 
        function() {
            action(b)
        },
        false)
    }
};